//microcommand define quote.list
inject(["database"])
signature("", { showWarnings: true, checkArgCount: true, checkUnknown: true })
option("user", "-u <user:user>")
option("channel", "-c <channel:channel>")
option("channel", "-C", { value: false })
option("reverse", "-r")
option("offset", "-o <id:posint>")
const limit = 16
action(async ({ session, options }) => {
const filter = {}
if (options.user) filter.uid = options.user.replace(/^[^:]*:/, "")
if (options.channel === false) filter.uid ??= session.userId
else if (options.channel) filter.gid = options.channel.replace(/^[^:]*:/, "")
else filter.gid = session.channelId
if (options.offset) filter.id = { $gte: options.offset }
const list = await ctx.database.get("hidden_story", filter, { limit, sort: { id: options.reverse ? "desc" : "asc" } })
if (!list.length) return session.text(".no-result")
let next = null
if (list.length === limit) {
next = list.pop().id
}
const nameCache = Object.create(null)
let result = (await Promise.all(list.map(async e => {
  e.groupName = await (nameCache[e.gid] ??= await (nameCache[e.gid] = session.bot.getChannel(e.gid).then(g => g.name, () => "群聊")))
  e.nickname = await (nameCache[e.gid + ":" + e.uid] ??= await (nameCache[e.gid + ":" + e.uid] = session.bot.getGuildMember(e.gid, e.uid).then(m => m.nick || m.user.name || m.user.username, () => "QQ用户")))
  e.datetime = new Date(e.timestamp).toLocaleString()
  e.content = h.parse(e.content)
  return session.text(".result", e)
}))).join("<br/><br/>")
if (next !== null) result += "<br/><br/>" + session.text(".continue", [next])
return session.execute({ tokens: [{ content: "imagify", inters: [], terminator: " " }, { content: result, quoted: true, inters: [], terminator: "" }] })
})
locale("zh-CN", {
  "description": "显示当前群内的黑历史列表",
  "options": {
    "user": "指定用户",
    "channel": "指定频道(群)",
    "channel.false": "显示自己(或指定用户)在所有群的记录",
    "reverse": "倒序显示",
    "offset": "从此序号开始显示",
  },
  "messages": {
    "no-result": "无结果…",
    "result": "[#{id}] {nickname} ({uid}) 于 {datetime} 在 {groupName} ({gid})\n{content}",
    "continue": "使用选项 -o {0} 查看下一页",
  },
})